package com.a01_2021.thousandstimes.general.controller;

import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.exception.CustomExceptionType;
import com.a01_2021.thousandstimes.general.entity.SysRole;
import com.a01_2021.thousandstimes.general.service.SysMenuService;
import com.a01_2021.thousandstimes.general.service.SysRoleService;
import com.a01_2021.thousandstimes.utils.AjaxResponse;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author FRQ
 * @since 2021-02-22
 */
@RestController
@RequestMapping("/sys-role")
public class SysRoleController {

    @Resource
    SysRoleService sysRoleService;

    @Resource
    SysMenuService sysMenuService;

    /**
     * 添加角色
     *
     * @param roleName 角色名
     * @return 返还AjaxResponse
     * @throws CustomException 当角色已存在时抛出自定义异常
     */
    @PreAuthorize("hasAuthority('sys-role-addRole')")
    @DS("#header.instanceid")
    @PostMapping("addRole")
    public AjaxResponse addRole(String roleName, String roleDes) throws CustomException {
        if (StringUtils.isEmpty(roleName))//过滤传入角色名变量是否为空
            return AjaxResponse.error(CustomExceptionType.USER_INPUT_ERROR, "角色名不得为空");
        SysRole sysRole = sysRoleService.addRole(roleName, roleDes);
        return AjaxResponse.success(sysRole);
    }

    /**
     * 移除角色
     *
     * @param roleId 角色id
     * @return 返还AjaxResponse
     */
    @PreAuthorize("hasAuthority('sys-role-removeRole')")
    @DS("#header.instanceid")
    @PostMapping("removeRole")
    public AjaxResponse removeRole(long roleId) throws CustomException {
        sysRoleService.removeRoleById(roleId);
        return AjaxResponse.success(null, "成功删除角色");
    }

    /**
     * 强制移除角色
     *
     * @param roleId 角色id
     * @return 返还AjaxResponse
     */
    @PreAuthorize("hasAuthority('sys-role-forceRemoveRole')")
    @DS("#header.instanceid")
    @PostMapping("forceRemoveRole")
    public AjaxResponse forceRemoveRole(long roleId) throws CustomException {
        sysRoleService.forceRemoveRoleById(roleId);
        return AjaxResponse.success(null, "成功强制删除角色");
    }


    /**
     * 设置角色具有权限
     *
     * @param roleId      角色id
     * @param setMenuList 权限列表（列表内以权限id标识
     * @return 返还AjaxResponse
     * @throws CustomException 当设置的权限不存在时抛出CustomException
     */
    @PreAuthorize("hasAuthority('sys-role-setMenuToRole')")
    @DS("#header.instanceid")
    @PostMapping("setMenuToRole")
    public AjaxResponse setMenuToRole(long roleId, @RequestParam(value = "setMenuList") Set<Long> setMenuList) throws CustomException {
        if (setMenuList == null || setMenuList.isEmpty()) {
            //若传入角色列表为空
            return AjaxResponse.error(CustomExceptionType.USER_INPUT_ERROR, "角色权限不得为空");
        }
        Map<String, Object> resultMap = sysRoleService.setMenuToRole(roleId, setMenuList);
        return AjaxResponse.success(resultMap);
    }

    /**
     * 获取系统全部角色
     *
     * @return 返还AjaxResponse
     */
    @PreAuthorize("hasAuthority('sys-role-getAllRole')")
    @DS("#header.instanceid")
    @PostMapping("getAllRole")
    public AjaxResponse getAllRole() {
        List<SysRole> sysRoleList = sysRoleService.getAllRole();
        return AjaxResponse.success(sysRoleList);
    }

    /**
     * 通过角色id复制角色
     *
     * @param roleId   角色id
     * @param roleName 新角色名
     * @return AjaxResponse
     * @throws CustomException 当角色不存在，角色重名时抛出该异常
     */
    @PreAuthorize("hasAuthority('sys-role-copyRole')")
    @DS("#header.instanceid")
    @PostMapping("copyRole")
    public AjaxResponse copyRole(long roleId, String roleName, String roleDec) throws CustomException {
        if (StringUtils.isEmpty(roleName)) {
            return AjaxResponse.error(CustomExceptionType.USER_INPUT_ERROR, "角色名不可为空");
        }
        Map<String, Object> resultMap = sysRoleService.copy(roleId, roleName, roleDec);
        return AjaxResponse.success(resultMap);
    }

    /**
     * 使用权限id查询角色
     *
     * @param menuId 权限id
     * @return Ajaxresonse
     * @throws CustomException 当权限不存在抛出该异常
     */
    @PreAuthorize("hasAuthority('sys-role-getRoleByMenu')")
    @DS("#header.instanceid")
    @PostMapping("getRoleByMenu")
    public AjaxResponse getRoleByMenu(long menuId) throws CustomException {
        List<SysRole> sysRoleList = sysRoleService.getRoleByMenu(menuId);
        return AjaxResponse.success(sysRoleList);
    }
}
